<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/23
 * Time: 14:53
 */

namespace app\api\model;
use think\Model;
use think\Log;
use think\Db;
use app\common\lib\exception\ApiException;

class LessonNumTime extends Model
{
    /**
     * @ 回显课程时间信息
     * @param id int 必传 课程id
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */

    public function getLessonTime(){

        $data = $_POST;
        if(empty($data)){
            throw new ApiException('数据不合法',404);
        }
        // 必传id 课程详情
        if(empty($data['id'])){
            throw new ApiException('id is not found',404);
        }
        try{
            $res = Db::name('lesson_num_time')->where('lesson_id',$data['id'])->order("lesson_number")->select();
            $lesson=Db::name('lesson')->where('id',$data['id'])->find();
            foreach($res as $key => $val){
                $res[$key]['complete_lesson_num'] = $lesson['com_lesson_num'];
            }
            return show(config('code.success'),'获取数据成功',$res,200);
        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException('数据不合法',400);
        }
    }

    /**
     * 修改每次课时上课时间
     * @param lesson_id int 课程id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function editLessonTime(){

        if(empty($_GET)){
            throw new ApiException('数据不合法',401);
        }

        try{
            $header = request()->header();

            if(strtolower($header['apptype']) == 'ios'){
                $arr = $_GET['lesson_number'];
            }else if(strtolower($header['apptype']) == 'android'){
                $_GET['lesson_start_time'] = json_decode($_GET['lesson_start_time']);
                $_GET['lesson_end_time'] = json_decode($_GET['lesson_end_time']);
                $arr = json_decode($_GET['lesson_number']);
            }

            $result = [];
            foreach($arr as $k => $v){
                $data['lesson_id'] = $_GET['lesson_id'];
                $data['lesson_number'] = $v;
                $data['lesson_start_time'] = strtotime($_GET['lesson_start_time'][$k]);
                $data['lesson_end_time'] = strtotime($_GET['lesson_end_time'][$k]);
                $result[]= Db::name('lesson_num_time')
                    ->where(array('lesson_id'=>$data['lesson_id'],'lesson_number'=>$data['lesson_number']))
                    ->update($data);
            }

//            if(in_array(1,$result)){
//                return show(config('code.success'),'操作成功！');
//            }else{
//                return show(config('code.error'),'操作失败！');
//            }
            return show(config('code.success'),'修改成功！');

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }


}